package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessAction;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.MsgTemplateWxConfigure;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @describe
 */
public abstract class MsgTemplateWxConfigureDao extends HibernateSupportDelegateDAO<MsgTemplateWxConfigure> {

    public MsgTemplateWxConfigureDao() {
        this.setKeyField("configureId");
    }

    @DAOMethod(sql = " from MsgTemplateWxConfigure where configureId=:configureId")
    public abstract MsgTemplateWxConfigure getMsgTemplateWxConfigure(@DAOParam("configureId") Long configureId);

    @DAOMethod(sql = " delete from MsgTemplateWxConfigure where templateId=:templateId")
    public abstract void deleteMsgTemplateWxConfigureByTemplateId(@DAOParam("templateId") Long templateId);

    /**
     *
     *@describe得到消息模板的消息类型列表
     *@throws
     *@author zhengcb10708
     *@date 2020/9/18 15:51
     */
    public List<MsgTemplateWxConfigure> getMsgTemplateWxConfigureList(final Long  msgTemplateId) {
        HibernateStatelessResultAction<List<MsgTemplateWxConfigure>> action =
                new AbstractHibernateStatelessResultAction<List<MsgTemplateWxConfigure>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append(" select distinct  a.app_id appId,a.configure_id configureId, a.template_id templateId, a.subscription_template_id subscriptionTemplateId,b.tpl_code subscriptionTemplateCode, " );
                        stringBuffer.append(" a.subscription_jump_url subscriptionJumpUrl, a.subscription_first subscriptionFirst,  " );
                        stringBuffer.append(" a.subscription_first_color subscriptionFirstColor,a.subscription_keyword1_color subscriptionKeyword1Color,  " );
                        stringBuffer.append(" a.subscription_keyword2_color subscriptionKeyword2Color,a.subscription_keyword3_color subscriptionKeyword3Color,  " );
                        stringBuffer.append(" a.subscription_keyword4_color subscriptionKeyword4Color,a.subscription_keyword5_color subscriptionKeyword5Color,  " );
                        stringBuffer.append(" a.subscription_remark_color subscriptionRemarkColor,  " );
                        stringBuffer.append(" a.subscription_keyword1 subscriptionKeyword1, a.subscription_keyword2 subscriptionKeyword2, a.subscription_keyword3_flag subscriptionKeyword3Flag, a.subscription_keyword3 subscriptionKeyword3, " );
                        stringBuffer.append(" a.subscription_keyword4_flag subscriptionKeyword4Flag, a.subscription_keyword4 subscriptionKeyword4, " );
                        stringBuffer.append(" a.subscription_keyword5_flag subscriptionKeyword5Flag, a.subscription_keyword5 subscriptionKeyword5,  " );
                        stringBuffer.append(" a.subscription_remark subscriptionRemark, a.applets_template_id appletsTemplateId, a.applets_template_jump_page appletsTemplateJumpPage, " );
                        stringBuffer.append(" a.applets_default_language appletsDefaultLanguage, a.applets_variable1_flag appletsVariable1Flag,a.applets_variable1_key appletsVariable1Key,a.applets_variable1_value appletsVariable1Value," );
                        stringBuffer.append(" a.applets_variable2_flag appletsVariable2Flag, a.applets_variable2_key appletsVariable2Key, a.applets_variable2_value appletsVariable2Value," );
                        stringBuffer.append(" a.applets_variable3_flag appletsVariable3Flag, a.applets_variable3_key appletsVariable3Key, a.applets_variable3_value appletsVariable3Value, " );
                        stringBuffer.append(" a.applets_variable4_flag appletsVariable4Flag, a.applets_variable4_key appletsVariable4Key, a.applets_variable4_value appletsVariable4Value, " );
                        stringBuffer.append(" a.applets_variable5_flag appletsVariable5Flag, a.applets_variable5_key appletsVariable5Key, a.applets_variable5_value appletsVariable5Value, " );
                        stringBuffer.append(" a.gmt_create gmtCreate, a.gmt_modified gmtModified " );
                        stringBuffer.append(" from msg_template_wx_configure a ,msg_tpl_wx b where (a.subscription_template_id = b.wx_tpl_id or a.applets_template_id = b.wx_tpl_id) and  a.template_id=:templateId " );
                        stringBuffer.append(" order by a.configure_id desc limit 1 " );


                        Query query = ss.createSQLQuery(stringBuffer.toString());
                        query.setParameter("templateId", msgTemplateId);
                        query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                        List<Map> list = query.list();
                        List<MsgTemplateWxConfigure> voList = new ArrayList<MsgTemplateWxConfigure>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, MsgTemplateWxConfigure.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    /**
     *
     *@describe 新增消息模板配置
     *@throws
     *@author zhengcb10708
     *@date 2020/9/21 15:59
     */
    public void addMsgTemplateWxConfigure(final MsgTemplateWxConfigure msgTemplateWxConfigure) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                ss.insert(msgTemplateWxConfigure);
            }
        });
    }

    /**
     *
     *@describe 修改消息模板配置
     *@throws
     *@author zhengcb10708
     *@date 2020/9/21 14:14
     */
    public void updateMsgTemplateWxConfigure(final MsgTemplateWxConfigure msgTemplateWxConfigure) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                //更新模板配置信息表
                ss.update(msgTemplateWxConfigure);
            }
        });
    }

}
